package com.families.controller;

import java.util.concurrent.TimeUnit;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.families.entity.Result;
import com.families.entity.User;
import com.families.service.LoginService;
import com.families.util.ObjectCheck;
import com.sun.mail.handlers.message_rfc822;

@Controller
@RequestMapping("/shiro")
public class LonginController {

	@Autowired
	private LoginService userServiceInterface;
	@Autowired
	private RedisTemplate redisTemplate;

	//添加日志
	private static final Logger logger = LoggerFactory.getLogger(LonginController.class);

	//登录
	@RequestMapping("/login")
	@ResponseBody
	public Result login(User user) {
		if(ObjectCheck.isEmpty(user)) {
			logger.error("传递参数为空");
			return new Result(Result.ERROR, "登录失败", null);
		}
		String login="";
		try {
			logger.info("登录请求参数：{}",JSON.toJSONString(user));
			login=userServiceInterface.login(user);
			logger.info("登录返回参数：{}",login);
			if("登陆成功".equals(login)) {
				logger.info("登陆成功");
				SecurityUtils.getSubject().getSession().setTimeout(1800000);
				User userInfo = (User) SecurityUtils.getSubject().getPrincipal();
				//半小时超时
			    redisTemplate.opsForValue().set(user.getUserName(),userInfo,30,TimeUnit.MINUTES);
				return new Result(Result.SUCCESS, "登陆成功",null);
			}
			logger.info("登陆失败");
			return new Result(Result.ERROR, login,null);

		} catch (Exception e) {
			// TODO: handle exception
			logger.info("登陆异常",e);
			logger.info("登陆异常:{}",login);
			return new Result(Result.ABNORMAL,login ,null);
		}

	}

	//注册
	@RequestMapping("/sign")
	@ResponseBody
	public Result sign( User user) {
		try {
			logger.info("注册请求参数：{}",JSON.toJSONString(user));
			if(ObjectCheck.isEmpty(user)) {
				logger.error("传递参数为空");
				return new Result(Result.ERROR, "注册失败", null);
			}
			if(!user.getPassWord().equals(user.getPassWordConfirm())) {
				logger.error("两次密码不一致");
				return new Result(Result.ERROR, "两次密码不一致", null);
			}
			User oldUser=userServiceInterface.signNameComparison(user.getUserName());
			logger.info("注册判断用户是否存在返回参数：{}",JSON.toJSONString(oldUser));
			if(!ObjectCheck.isEmpty(oldUser)) {
				logger.error("用户已存在");
				return new Result(Result.DATAEXISTS, "用户已存在", null);
			}
			int sign=userServiceInterface.sign(user);
			logger.info("注册返回参数：{}",sign);
			if(sign<1) {
			    return new Result(Result.ERROR, "注册失败", null);
			}
			return new Result(Result.SUCCESS, "注册成功", null);
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("注册异常",e);
			return new Result(Result.ERROR, "注册出现异常", null);
		}
	}  
	//验证用户名是否存在
	@RequestMapping("/signNameComparison")
	@ResponseBody
	public Result signNameComparison( String userName) {
		try {
			logger.info("验证用户名是否存在请求参数：{}",userName);
			User user=userServiceInterface.signNameComparison(userName);
			logger.info("验证用户名是否存在返回参数：{}",JSON.toJSONString(user));
			if(null!=user) {
				logger.info("用户已存在");
				return new Result(Result.ERROR, "用户已存在", null);
			}
			 return new Result(Result.SUCCESS, "用户不存在", null);
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("验证用户名是否存在异常",e);
			return new Result(Result.ABNORMAL, "验证用户名是否存在异常", null);
		}
	}  

	//注销
	@RequestMapping("/logout")
	@ResponseBody
	public Result logout(String source) {
		try {
			System.out.println(source);
			userServiceInterface.logou();
			return new Result(Result.SUCCESS,"注销成功",null);
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("注销失败",e);
			return new Result(Result.ERROR,"注销失败",null);
		}
	}
}
